﻿/*****************************************
 * 
 *  copyright (c) by ylshen
 * 
 *  author : ylshen (syl.lucky@gmail.com)
 * 
 **************************************** */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using JYStockAnalyser.ClientLib;
using JYStockAnalyser.Face;

namespace JYStockAnalyser.WinUI
{
    public partial class FrmMain : Form
    {
        static FrmMain m_instance;

        bool m_isGuest = false;

        public bool IsGuest
        {
            get { return m_isGuest; }
            set { m_isGuest = value; }
        }

        public void Guest()
        {
            m_isGuest = true;
            this.toolStrip1.Enabled = false;
            this.menuStrip1.Enabled = false;
        }
       

        public static FrmMain Instance
        {
            get
            {
                if (m_instance == null)
                {
                    m_instance = new FrmMain();
                }
                return m_instance;
            }
        }

        DateTime m_lastOperationStartTime;

        public void BeginLogTime()
        {
            m_lastOperationStartTime = DateTime.Now;
        }

        public void EndLogTime()
        {
            TimeSpan ts = DateTime.Now - m_lastOperationStartTime;
            lblUsedTime.Text = String.Format("耗时 {0} 毫秒", ts.TotalMilliseconds);
        }

        public void AddChidren(Form frm)
        {
            if (this.ActiveMdiChild != null)
                this.ActiveMdiChild.Close();

            frm.WindowState = FormWindowState.Maximized;
            frm.MdiParent = this;
            this.SuspendLayout();
            frm.Show();
            this.ResumeLayout();         
        }

        public FrmMain()
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            this.WindowState = FormWindowState.Maximized;
            InitializeComponent();
        }     

        private void FrmMain_Load(object sender, EventArgs e)
        {
            m_beginShowMessage();
            this.Text = Configer.GetApplicationName();

            SetDefaultForm();        
        }

        /// <summary>
        /// 设置为默认窗体
        /// </summary>
        public void SetDefaultForm()
        {
            this.AddChidren(new FrmBG());
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.AddChidren(new FrmBG());
        }

        private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new FrmChangePassword().Show();
        }

        private void 查看个人信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new FrmShowUserInfo().Show();
        }

        void frm_Resize(object sender, EventArgs e)
        {
            ((Form)sender).FormBorderStyle = FormBorderStyle.None;
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            this.AddChidren(new FrmSingleStockAnalyse());
        }

        private void 复利计算器ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.AddChidren(new FrmCompoundGrowthRateComputer());
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            this.AddChidren(new FrmCompoundGrowthRateComputer());
        }

        private void 计算器ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("calc.exe");
            }
            catch(System.Exception)
            {
            }
        }

        private void 多指标对比分析ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.AddChidren(new FrmMultipleGuideLineAnalysecs());
        }

        private void 多股对比分析ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.AddChidren(new FrmMultipleStocksAnalyse());
        }

        private void 个股财务指标分析ToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
        }

        private void FrmMain_Shown(object sender, EventArgs e)
        {
            FrmLogin frm = new FrmLogin();
            frm.StartPosition = FormStartPosition.CenterParent;            
            frm.ShowDialog();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            this.AddChidren(new FrmEstimate());
        }

        private void 成长估值器ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.AddChidren(new FrmEstimate());
        }

        private void 多指标对比分析财务ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.AddChidren(new FrmFinancialMultipleGuideLineAnalyse());
        }

        private void 多股对比分析财务ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.AddChidren(new FrmFinancialMultipleStocksAnalyse());
        }

        private void 个股分析ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.AddChidren(new FrmSingleStockAnalyse());
        }

        private void 个股市场数据分析ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.AddChidren(new FrmFinancialAnalyse());
        }

        private void 利润增速排序选股ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.AddChidren(new FrmProfitGrowthSpeedOrder());
        }

        private void toolStripButton9_Click(object sender, EventArgs e)
        {
            this.AddChidren(new FrmSecurityORder());
        }

        private void 安全级别排序选股ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.AddChidren(new FrmSecurityORder());
        }

        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            this.AddChidren(new FrmProfitGrowthSpeedOrder());
        }

        void m_beginShowMessage()
        {
            thShowMessage = new Thread(new ThreadStart(delegate()
                {
                    StockManageFace face = new StockManageFace();
                    string message = face.GetMainShowMessage();                  
                    float messageWidth = this.lblMessage.CreateGraphics().MeasureString(message, lblMessage.Font).Width;         

                 
                    while (true)
                    {
                        for (int i = lblMessage.Width; i > -((int)messageWidth) -10; i -= 5)
                        {
                            this.lblMessage.Refresh();
                            this.lblMessage.CreateGraphics().DrawString(message, lblMessage.Font, Brushes.Black, i, 1);                        
                            Thread.Sleep(200);
                        }                    
                    }

                    
                }));

            thShowMessage.Start();
        }

        Thread thShowMessage;       

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            thShowMessage.Abort();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            this.AddChidren(new FrmMultipleStocksAnalyse());
        }
    }
}
